﻿using UnityEngine;
using System.Collections;
using BehaviorDesigner.Runtime;
using BehaviorDesigner.Runtime.Tasks;

public class MyCanseeCondition : Conditional {

	public Transform[] targets;
	public float fieldOfViewAngle = 90;
	public float distance = 7f;
	public SharedTransform target;

	public override TaskStatus OnUpdate ()
	{
		if (targets == null) {
			return TaskStatus.Failure;
		}
		if (targets.Length == 0) {
			return TaskStatus.Failure;
		}

		foreach (var target in targets) {
			float dis = (target.position - transform.position).magnitude;
			float angle = Vector3.Angle(transform.forward, target.position - transform.position);

			if(dis < distance && angle < fieldOfViewAngle *0.5){
				this.target.Value = target;
				return TaskStatus.Success;
			}
		}

		return TaskStatus.Failure;
	}
}
